package cn.tedu.charging.order.config;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfiguration {
    public static  final String EXCHANGE_NAME="charging_order_exchange";
    public static  final String QUEUE_NAME="charging_order_queue";
    public static  final String ROUTING_KEY="charging_order_routing_key";

    private static final String DEAD_LETTER_EXCHANGE_NAME = "dead_letter_charging_order_exchange";

    public static final String DEAD_LETTER_QUEUE_NAME = "dead_letter_charging_order_queue";

    private static final String DEAD_LETTER_ROUTING_KEY = "dead_letter_charging_order_routing_key";
    @Bean
    public DirectExchange orderExchange(){
        Boolean durable=true;
        Boolean autoDel=false;
        return  new DirectExchange(EXCHANGE_NAME,durable,autoDel);
    }
    @Bean
    public Queue orderQueue(){
        Map<String,Object> args=new HashMap<>();
        Integer ttl=2*60*1000;
        args.put("x-message-ttl",ttl);
        args.put("x-dead-letter-exchange",DEAD_LETTER_EXCHANGE_NAME);
        args.put("x-dead-letter-routing-key",DEAD_LETTER_ROUTING_KEY);
        args.put("x-queue-mode","lazy");

        Boolean durable=true;
        return new Queue(QUEUE_NAME,durable,true,false,args);
    }
    @Bean
    public Binding orderBinfinf(){
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with(ROUTING_KEY);


    }
    @Bean
    public DirectExchange deadLetterExchange(){
        Boolean durable=true;
        Boolean autoDel=false;
        return  new DirectExchange(DEAD_LETTER_EXCHANGE_NAME,durable,autoDel);
    }
    @Bean
    public Queue deadLetterQueue(){
        Boolean durable=true;
        return new Queue(DEAD_LETTER_QUEUE_NAME,durable);
    }
    @Bean
    public Binding deadLetterBinfinf(){
        return BindingBuilder.bind(deadLetterQueue()).to(deadLetterExchange()).with(DEAD_LETTER_ROUTING_KEY);
    }
}